//#define __DEBUG__
// by Xeno
#define THIS_FILE "x_revive\xr_f\fn_drag.sqf"
#include "xr_macros.sqf"

private ["_dragee", "_unit", "_found_anim", "_sdone", "_isdragging"];
_dragee = __pGetVar(GVARXR(cursorTarget));
//_dragee = _this select 0;
//_dragee setVariable [QGVARXR(pluncon), true];
//__pSetVar [QGVARXR(cursorTarget), _dragee];

_isdragging = __pGetVar(GVARXR(is_dragging));
if (!isNil "_isdragging" && {_isdragging}) exitWith {};

if (isNull _dragee|| {!alive player}) exitWith {};

__pSetVar [QGVARXR(pisinaction), true];
__pSetVar [QGVARXR(is_dragging), true];

if (stance player == "PRONE") exitWith {
	[_dragee] spawn FUNCXR(dragprone);
};

_unit = player;
private "_name_dragee";
_name_dragee = (localize "STR_DOM_MISSIONSTRING_906");
if (alive _dragee && {name _dragee != (localize "STR_DOM_MISSIONSTRING_906")}) then {_name_dragee = name _dragee};
 
GVARXR(drag_keyDownEHId) = (findDisplay 46) displayAddEventHandler ["KeyDown", {_this call FUNCXR(dragkeydown)}];
_dragee setVariable [QGVARXR(dragged), true, true];

_unit playMoveNow "acinpknlmstpsraswrfldnon";
sleep 2;

[QGVARXR(a3), _dragee] call d_fnc_NetCallEvent;
_dragee attachto [_unit,[0.1, 1.01, 0]];
//_dragee attachto [_unit,[0, 1.1, 0.092]];

sleep 0.02;
[QGVARXR(sd), _dragee] call d_fnc_NetCallEvent;

GVARXR(drag) = true;
sleep 0.02;


if (GVARXR(dropAction) != -3333) then {__PRA(GVARXR(dropAction));GVARXR(dropAction) = -3333};
GVARXR(dropAction) = player addAction [format["<t color='#FF0000'>Drop %1</t>",_name_dragee], {_this call FUNCXR(drop_body)}, [_dragee, 0], 0, false, true];
GVARXR(carryAction) = player addAction [format["<t color='#FF0000'>Carry %1</t>",_name_dragee], {_this spawn FUNCXR(carry)}, _dragee, 0, false, true];
sleep 1;

_found_anim = false;

_animsd = ["acinpknlmstpsraswrfldnon","acinpknlmwlksraswrfldb"];

while {GVARXR(drag)} do {
	if (!_found_anim && {animationState player in _animsd}) then {
		_found_anim = true;
	};
	if (!alive _dragee || {!__GV(_dragee,GVARXR(pluncon))}) exitWith {
		__PRA(GVARXR(dropAction));
		GVARXR(dropAction) = -3333;
		if (GVARXR(carryAction) != -3333) then {
			__PRA(GVARXR(carryAction));
			GVARXR(carryAction) = -3333;
		};
		detach _dragee;
		sleep 0.5;
		if ((alive _dragee && {__GV(_dragee,GVARXR(pluncon))}) || {!alive _dragee}) then {
			[QGVARXR(wn), [_dragee,101]] call d_fnc_NetCallEvent;
		} else {
			[QGVARXR(wn), [_dragee,102]] call d_fnc_NetCallEvent;
		};
		[QGVARXR(swmnon), _unit] call d_fnc_NetCallEvent;
		sleep 1;
		GVARXR(drag) = false;
	};
	
	if (_found_anim && {!((animationState _unit) in _animsd)}) exitWith {
		__PRA(GVARXR(dropAction));
		GVARXR(dropAction) = -3333;
		if (GVARXR(carryAction) != -3333) then {
			__PRA(GVARXR(carryAction));
			GVARXR(carryAction) = -3333;
		};
		detach _dragee;
		sleep 0.5;
		if ((alive _dragee && {__GV(_dragee,GVARXR(pluncon))}) || {!alive _dragee}) then {
			[QGVARXR(wn), [_dragee,101]] call d_fnc_NetCallEvent;
		} else {
			[QGVARXR(wn), [_dragee,102]] call d_fnc_NetCallEvent;
		};
		[QGVARXR(swmnon), _unit] call d_fnc_NetCallEvent;
		sleep 1;
		GVARXR(drag) = false;
	};

	if (!alive _unit) exitWith {
		__PRA(GVARXR(dropAction));
		GVARXR(dropAction) = -3333;
		if (GVARXR(carryAction) != -3333) then {
			__PRA(GVARXR(carryAction));
			GVARXR(carryAction) = -3333;
		};
		detach _unit;
		if ((alive _dragee && {__GV(_dragee,GVARXR(pluncon))}) || {!alive _dragee}) then {
			[QGVARXR(wn), [_dragee,101]] call d_fnc_NetCallEvent;
		} else {
			[QGVARXR(wn), [_dragee,102]] call d_fnc_NetCallEvent;
		};
		[QGVARXR(swmnon), _unit] call d_fnc_NetCallEvent;
		sleep 1;
		GVARXR(drag) = false;
	};
	
	if (isNull _dragee) then {
		__PRA(GVARXR(dropAction));
		GVARXR(dropAction) = -3333;
		if (GVARXR(carryAction) != -3333) then {
			__PRA(GVARXR(carryAction));
			GVARXR(carryAction) = -3333;
		};
		detach _unit;
		[QGVARXR(swmnon), _unit] call d_fnc_NetCallEvent;
		sleep 1;
		GVARXR(drag) = false;
	};
	sleep 0.1;
};

__pSetVar [QGVARXR(pisinaction), false];
__pSetVar [QGVARXR(is_dragging), false];
_dragee setVariable [QGVARXR(dragged), false, true];

(findDisplay 46) displayRemoveEventHandler ["KeyDown", GVARXR(drag_keyDownEHId)];

if (GVARXR(carryAction) != -3333) then {
	__PRA(GVARXR(carryAction));
	GVARXR(carryAction) = -3333;
};